<template>
  <a-modal
    class="qrcode-modal"
    width="500px"
    :closable="false"
    :visible="visible"
    :footer="null"
    @cancel="handleCancel"
  >
    <div class="qrcode-modal__body">
      <div class="title" :title="title">
        {{ title }}
      </div>
      <img class="qrcode-img" :src="dataUrl" :alt="$t('all.autotext858')">
      <a-button class="btn--download" type="primary" @click="downloadQrcode">{{ $t('all.autotext121') }}</a-button>
    </div>
  </a-modal>
</template>

<script>
import { dataUrlToBlob, downloadBlob } from '@/utils/util'

export default {
  name: 'qrcode-modal',
  props: {
    dataUrl: {
      type: String,
      default: ''
    },
    researchCenter: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    title () {
      const { researchCenterNo, orgName } = this.researchCenter
      return `${researchCenterNo}-${orgName}`
    }
  },
  data () {
    return {
      visible: false
    }
  },
  methods: {
    setVisible (visible) {
      this.visible = visible
    },
    handleCancel () {
      this.visible = false
    },
    downloadQrcode () {
      const blob = dataUrlToBlob(this.dataUrl)
      const fileType = blob.type.split('/')[1]
      downloadBlob(`${this.title}.${fileType}`, blob)
    }
  }
}
</script>

<style scoped lang="less">
.qrcode-modal {
  .qrcode-modal__body {
    display: flex;
    flex-direction: column;
    align-items: center;
    .title {
      width: 100%;
      color: #4D5C6F;
      font-size: 24px;
      overflow: hidden;
      text-overflow: ellipsis;
      text-align: center;
      white-space: nowrap;
    }
    .qrcode-img {
      width: 230px;
      height: 230px;
      margin: 30px 0;
    }
    .btn--download {
      width: 200px;
    }
  }

}

</style>
